﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Evision.Insure.SYSYB;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using Zlsy.Core.Json;
using Microsoft.VisualBasic;
using Evision.Insure;
using System.Windows.Forms;
using Zlsy.Core;
using Evision.Insure.LNSZYB;

namespace ZLSOFT.HIS.ZLMediCare
{
    /// <summary>
    /// 北京医保卡读取类
    /// </summary>
    public class CardInsure
    {
        public string ErrMessage = "";//用于返回错误信息
        private static cls_SYSYB sysyb = new cls_SYSYB(); //市医保接口
        private static cls_LNSZYB lnsyb = new cls_LNSZYB();//省医保接口
        private static ILogger __log = Log.GetLog("sysyb");
        private static ILogger _log = Log.GetLog("lnsyb");
        public int cardtype;
        public ADODB.Connection gcnOracle;
        private bool CheckOutputState(XmlDocument xmlDoc)
        {
            return true;
        }
        //选择医保类型
        public void ShowType(int type) {
            this.cardtype = type;
        }
        //打开医保接口
        public bool OpenDevice()
        {
            ErrMessage = "";///调用前清空错误信息
            try
            {
                object gobjComLib = Interaction.CreateObject("zl9Comlib.clsComLib");
                 gcnOracle = (ADODB.Connection)gobjComLib.GetType().InvokeMember("GetDBConnection", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod,
                   null, gobjComLib, new object[] { });
                return true;
            }
            catch 
            {
                ErrMessage = "医保接口初始化失败,请检查接口文件!";
                return false;
            }
        }
        //关闭医保接口
        public bool CloseDevice()
        {
            ErrMessage = "";///调用前清空错误信息
            try
            {
                sysyb.EndInsure();
                lnsyb.EndInsure();
                return true;
            }
            catch 
            {
                ErrMessage = "医保接口卸载失败,请检查接口文件!";
                return false;
            }
        }
        //获取医保卡信息
        public CareInfor GetCardInfo()
        {
            ErrMessage = "";///调用前清空错误信息
            try {
                if (this.cardtype == 1) {
                    sysyb.InitInsure(gcnOracle, 603);
                    //调用dll,获取信息
                    bool res = sysyb.ReadCard("03", out string ret);
                        if (res) {
                            __log.Info(ret);
                            OutputBase outputbase = JsonConvert.DeserializeObject<OutputBase>(ret);
                            CareInfor careinfo = new CareInfor();
                            careinfo.Ic_No = outputbase.output.baseinfo.psn_no;  //医保应用号
                            careinfo.cardinfoName = outputbase.output.baseinfo.psn_name;//姓名
                            careinfo.Birthday = outputbase.output.baseinfo.brdy.ToString("yyyyMMdd");//出生日期 19960929
                            careinfo.Sex = outputbase.output.baseinfo.gend == "1" ? "男" : "女";
                            careinfo.Card_Type = "603";//险类    
                            careinfo.Id_No = outputbase.output.baseinfo.certno;//身份证号 
                            __log.Info($"careinfo.Ic_No：{careinfo.Ic_No},careinfo.cardinfoName:{careinfo.cardinfoName},careinfo.Birthday:{careinfo.Birthday},careinfo.Sex:{careinfo.Sex},careinfo.Card_Type:{careinfo.Card_Type},careinfo.Card_No:{careinfo.Card_No},careinfo.Id_No:{careinfo.Id_No}");
                            return careinfo;
                        }
                    }
                    if (this.cardtype == 2) {
                    lnsyb.InitInsure(gcnOracle, 604);
                    //调用dll,获取信息
                    bool res = lnsyb.ReadCard("03", out string ret);
                        if (res) {
                            _log.Info(ret);
                            OutputBase outputbase = JsonConvert.DeserializeObject<OutputBase>(ret);
                            CareInfor careinfo = new CareInfor();
                            careinfo.Ic_No = outputbase.output.baseinfo.psn_no;  //医保应用号
                            careinfo.cardinfoName = outputbase.output.baseinfo.psn_name;//姓名
                            careinfo.Birthday = outputbase.output.baseinfo.brdy.ToString("yyyyMMdd");//出生日期 19960929
                            careinfo.Sex = outputbase.output.baseinfo.gend == "1" ? "男" : "女";
                            careinfo.Card_Type = "604";//险类    
                            careinfo.Id_No = outputbase.output.baseinfo.certno;//身份证号 
                            _log.Info($"careinfo.Ic_No：{careinfo.Ic_No},careinfo.cardinfoName:{careinfo.cardinfoName},careinfo.Birthday:{careinfo.Birthday},careinfo.Sex:{careinfo.Sex},careinfo.Card_Type:{careinfo.Card_Type},careinfo.Card_No:{careinfo.Card_No},careinfo.Id_No:{careinfo.Id_No}");
                            return careinfo;
                        }

                    }
                    if (this.cardtype == 3) {
                    sysyb.InitInsure(gcnOracle, 603);
                    //调用dll,获取信息
                    bool res = sysyb.ReadCard("01", out string ret);
                        if (res) {
                            _log.Info(ret);
                            OutputBase outputbase = JsonConvert.DeserializeObject<OutputBase>(ret);
                            CareInfor careinfo = new CareInfor();
                            careinfo.Ic_No = outputbase.output.baseinfo.psn_no;  //医保应用号
                            careinfo.cardinfoName = outputbase.output.baseinfo.psn_name;//姓名
                            careinfo.Birthday = outputbase.output.baseinfo.brdy.ToString("yyyyMMdd");//出生日期 19960929
                            careinfo.Sex = outputbase.output.baseinfo.gend == "1" ? "男" : "女";
                            careinfo.Card_Type = "603";//险类    
                            careinfo.Id_No = outputbase.output.baseinfo.certno;//身份证号 
                            _log.Info($"careinfo.Ic_No：{careinfo.Ic_No},careinfo.cardinfoName:{careinfo.cardinfoName},careinfo.Birthday:{careinfo.Birthday},careinfo.Sex:{careinfo.Sex},careinfo.Card_Type:{careinfo.Card_Type},careinfo.Card_No:{careinfo.Card_No},careinfo.Id_No:{careinfo.Id_No}");
                            return careinfo;
                        }
                    }
                if (this.cardtype == 4) {
                    sysyb.InitInsure(gcnOracle, 604);
                    //调用dll,获取信息
                    bool res = lnsyb.ReadCard("01", out string ret);
                    if (res) {
                        _log.Info(ret);
                        OutputBase outputbase = JsonConvert.DeserializeObject<OutputBase>(ret);
                        CareInfor careinfo = new CareInfor();
                        careinfo.Ic_No = outputbase.output.baseinfo.psn_no;  //医保应用号
                        careinfo.cardinfoName = outputbase.output.baseinfo.psn_name;//姓名
                        careinfo.Birthday = outputbase.output.baseinfo.brdy.ToString("yyyyMMdd");//出生日期 19960929
                        careinfo.Sex = outputbase.output.baseinfo.gend == "1" ? "男" : "女";
                        careinfo.Card_Type = "604";//险类    
                        careinfo.Id_No = outputbase.output.baseinfo.certno;//身份证号 
                        _log.Info($"careinfo.Ic_No：{careinfo.Ic_No},careinfo.cardinfoName:{careinfo.cardinfoName},careinfo.Birthday:{careinfo.Birthday},careinfo.Sex:{careinfo.Sex},careinfo.Card_Type:{careinfo.Card_Type},careinfo.Card_No:{careinfo.Card_No},careinfo.Id_No:{careinfo.Id_No}");
                        return careinfo;
                    }
                }
            } catch (Exception ex) {
                Log.RootLog.Error("读取医保卡错误", ex);
                ErrMessage = "医保卡信息读取失败,请检查接口文件! " + ex.Message;
                return null;
            }
            return null;
        }

    }
}
